package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.graphics.Color;
import android.os.Bundle;

import com.github.mikephil.charting.charts.BubbleChart;
import com.github.mikephil.charting.components.Description;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.data.BubbleData;
import com.github.mikephil.charting.data.BubbleDataSet;
import com.github.mikephil.charting.data.BubbleEntry;
import com.github.mikephil.charting.interfaces.datasets.IBubbleDataSet;

import java.util.ArrayList;
import java.util.List;

public class Main4Activity extends AppCompatActivity {
private BubbleChart bubble;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main4);
        setTitle("泡泡图");
        bubble=findViewById(R.id.bubble);
        float[] f1 ={2f,3f,4f,5f,6f,9f,1f};
        float [] f2={9f,8f,7f,6f,5f,1f,2f,3f};
        float [] f3={3f,8f,4f,5f,7f,6f,2f,1f};
        List<IBubbleDataSet> b = new ArrayList<>();
        BubbleDataSet b1=getDataSet(f1,"A");
        b1.setColor(Color.parseColor("#B4EEB4"));
        b.add(b1);
        BubbleDataSet b2=getDataSet(f2,"B");
        b2.setColor(Color.parseColor("#90EE90"));
        b.add(b2);
        BubbleDataSet b3=getDataSet(f3,"C");
        b3.setColor(Color.parseColor("#48D1CC"));
        b.add(b3);
        Description description = new Description();//更改右下角Description 显示内容
        description.setText("汇率泡泡图");
        description.setTextSize(12f);//字体大小
        bubble.setDescription(description);
        XAxis x=bubble.getXAxis();//控制x轴

        x.setPosition(XAxis.XAxisPosition.BOTTOM);
        x.setDrawAxisLine(false);

        BubbleData bubbleData=new BubbleData(b);
        bubble.setData(bubbleData);

        bubble.invalidate();

    }

    private BubbleDataSet getDataSet(float[] f1, String a) {
        List<BubbleEntry> data =new ArrayList<>();//Entry相当于坐标值
       for (int x=0;x<f1.length;x++){
           BubbleEntry bubbleEntry=new BubbleEntry(x,f1[x],x);
           data.add(bubbleEntry);
       }
       return new BubbleDataSet(data,a);
    }

}
